package com.baize.filedowndemo.conversationFile

import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.FileUtils
import android.util.Log
import android.webkit.MimeTypeMap
import androidx.annotation.RequiresApi
import java.io.File
import java.io.FileOutputStream
import kotlin.random.Random

class FileHelper {
  companion object {

    @RequiresApi(Build.VERSION_CODES.Q)
    fun uriToFileQ(context: Context, uri: Uri): File? =
      if (uri.scheme == ContentResolver.SCHEME_FILE) File(requireNotNull(uri.path))
      else if (uri.scheme == ContentResolver.SCHEME_CONTENT) { //把文件保存到沙盒
        val startTime = System.currentTimeMillis()
        val contentResolver = context.contentResolver
        val displayName = "${System.currentTimeMillis()}${Random.nextInt(0, 9999)}.${
          MimeTypeMap.getSingleton().getExtensionFromMimeType(contentResolver.getType(uri))
        }"
        val ios = contentResolver.openInputStream(uri)
        if (ios != null) {
          File("${context.cacheDir.absolutePath}/$displayName").apply {
            val fos = FileOutputStream(this)
            FileUtils.copy(ios, fos)
            Log.i("baize", "uriToFileQ: 耗时: ${System.currentTimeMillis() - startTime}")
            fos.close()
            ios.close()
          }
        } else null
      } else null
  }

}